﻿

using System;
using System.Reflection;
using System.Xml;
namespace HBase {
    public class InitServer {
        public static void start() {
            System.Threading.Tasks.Task.Factory.StartNew(() => {
                init();
            });
        }
        private static void init() {
            String path = "Conf/bean.config";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlElement root = doc.DocumentElement;
            XmlNodeList childNodes = root.ChildNodes;
            foreach( XmlNode node in childNodes ) {
                createNode(node);
            }
        }

        static Type  getType(String value) {

            var assemblyName = value.Substring(0, value.LastIndexOf("."));

            Assembly[] asss = AppDomain.CurrentDomain.GetAssemblies() ;

            foreach (Assembly asst in asss) {
                if (asst.GetName().Name == assemblyName) {

                    Type t = asst.GetType(value);
                    return t;

                }


            }
            String dllName = assemblyName + ".dll";
            String dllPath = AppDomain.CurrentDomain.BaseDirectory + dllName;
            Assembly ass = Assembly.LoadFile(dllPath);//要绝对路径
            Type type = ass.GetType(value);//必须使用名称空间+类名称
            return type;
            
            
        }
        private static void createNode(XmlNode node) {
            if( node.Attributes == null ) {
                return;
            }
            var className = node.Attributes["className"];
            var info = node.Attributes["info"];
            XmlAttribute p = node.Attributes["p"];
            String value = className.Value;
            try {

                Type type = getType(value);
                if ( node.Name == "task" ) {

                    var t = Activator.CreateInstance(type);
                    ZtTask task = (ZtTask)t;

                    createAndBegin(task ,node,p);
                    if( info != null ) {
                        String v = info.Value;
                        if( !String.IsNullOrEmpty(v) ) {
                            task.stat.className = v;
                        }
                    }
                } else if( node.Name == "factray" ) {
                    ZtFactray obj = ( ZtFactray ) Activator.CreateInstance(type);
               
                    if( p != null ) {
                        obj.setParam(p.Value.ToString());
                    }
                    ZtTask task = obj.create();
                    if( task != null ) {
                        createAndBegin(task ,node,p);
                    }
                }
            } catch( Exception e ) {
                LoggerCommon.errorFile(value + "创建异常" + e.Message ,e);
            }
        }
        static void createAndBegin(ZtTask task ,XmlNode node, XmlAttribute p ) {
            if (p != null) {
                task.p = p.Value.ToString();
            } 
            TaskPool.add(task); 
            TaskPool.start(task.GetType());
        }
    }
}
